package com.datastructure.test.minstring;

import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;

public class MinString {

    public static void main(String[] args) {
        MinString minString = new MinString();
        String[] strs = {"a","a","b"};
        String string = minString.minString(strs);
        System.out.println(string);
    }

    public String minString (String[] strs) {
        // write code here
        if(strs==null||strs.length==0){
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        PriorityQueue<String> stringPriorityQueue = new PriorityQueue<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return (o1+o2).compareTo(o2+o1);
            }
        });
        Collections.addAll(stringPriorityQueue,strs);
        for (int i = 0; i < strs.length; i++) {
            stringBuilder.append(stringPriorityQueue.poll());
        }
        return stringBuilder.toString();
    }
}
